<template>
  <div>
     <Row>
        <form enctype="multipart/form-data">
              <input type="file" icon="ios-download-outline" @change="onFileChange">
              <Button type="ghost" icon="ios-cloud-upload-outline" @click="onImport" >导入Excel</Button>
        </form>
       
        <Button type="primary" size="large" @click="onExport"  icon="ios-download-outline"></Icon>导出EXCEL</Button>
    </Row>
    <Table ref="table" class="bottom-table" :border="true" :stripe="true" :show-header="true" :data="tableData" :columns="tableColumns" ></Table>
    <Page class="bottom-page" :total="totalPage" :current="crtPage" :page-size="pageSize" show-total show-elevator @on-change="onChange"></Page>
    <Modal
        title="编辑level"
        v-model="editLevelModal" ok-text="提交" @on-ok="onSubmit">
      <Form  label-position="left" :model="formEdit" :label-width="150">
        <Form-item label="id">
            <Input v-model="formEdit.id" disabled ></Input>
        </Form-item>
        <Form-item label="exp">
            <Input v-model="formEdit.exp" ></Input>
        </Form-item>
        <Form-item label="addMoney">
            <Input v-model="formEdit.addMoney"></Input>
        </Form-item>
        <Form-item label="addVipExp">
            <Input v-model="formEdit.addVipExp"></Input>
        </Form-item>
        <Form-item label="betLevel">
            <Input v-model="formEdit.betLevel"></Input>
        </Form-item>
        <Form-item label="activePoint">
            <Input v-model="formEdit.activePoint"></Input>
        </Form-item>
        <Form-item label="betParam">
            <Input v-model="formEdit.betParam"></Input>
        </Form-item>
      </Form>
    </Modal>
  </div>
</template>
<script>

import {getLevels, getAllLevels, modifyLevel, importLevels} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
import {exportExcel, importExcel} from '@/common/excelUtil.js'
export default {
  data () {
    return {
      tableData: [],
      editLevelModal: false,
      filename: 'level',
      results: '',
      crtPage: 1,
      totalPage: 0,
      pageSize: 10,
      formEdit: {
        id: '',
        exp: '',
        addMoney: '',
        addVipExp: '',
        betLevel: '',
        activePoint: ''
      }
    }
  },
  created () {
    this.onSearch()
  },
  methods: {
    onSearch () {
      getLevels(this.crtPage, this.pageSize).then((data) => {
        if (!data) {
          return
        }
        this.tableData = []
        let LevelVO = getVO('LevelVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = LevelVO.decode(pv.data[i])
          vo.id = vo.id.toNumber()
          vo.exp = vo.exp.toNumber()
          vo.addMoney = vo.addMoney.toNumber()
          vo.addVipExp = vo.addVipExp.toNumber()
          vo.activePoint = vo.activePoint.toNumber()
          vo.betParam = vo.betParam.toFixed(3)
          this.tableData.push(vo)
        }

        this.totalPage = pv.intValues[1]
        console.log(this.tableData)
      })
    },
    onChange (item) {
      console.log(item)
      this.crtPage = item
      this.onSearch()
    },
    onUploadSuccess (response, file, fileList) {
      this.$Message.info('load success!!!')
      console.log(response)
      console.log(file)
      console.log(fileList)
    },
    showEditLevel (index) {
      let data = this.tableData[index]
      this.formEdit.id = data.id
      this.formEdit.exp = data.exp
      this.formEdit.addMoney = data.addMoney
      this.formEdit.addVipExp = data.addVipExp
      this.formEdit.betLevel = data.betLevel
      this.formEdit.activePoint = data.activePoint
      this.formEdit.betParam = data.betParam

      this.editLevelModal = true
    },
    onSubmit () {
      if (this.formEdit.id === '' || this.formEdit.exp === '' || this.formEdit.addMoney === '' || this.formEdit.addVipExp === '' || this.formEdit.betLevel === '' || this.formEdit.activePoint === '' || this.formEdit.betParam === '') {
        this.$Message.error('编辑参数不能为空!!!')
        return
      }

      let LevelVO = getVO('LevelVO')
      var levelVO = new LevelVO()
      levelVO.id = Number(this.formEdit.id)
      levelVO.exp = Number(this.formEdit.exp)
      levelVO.addMoney = Number(this.formEdit.addMoney)
      levelVO.addVipExp = Number(this.formEdit.addVipExp)
      levelVO.activePoint = Number(this.formEdit.activePoint)
      levelVO.betLevel = parseInt(this.formEdit.betLevel)
      levelVO.betParam = Number(this.formEdit.betParam)

      modifyLevel(levelVO).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          let len = this.tableData.length
          for (let i = 0; i < len; i++) {
            let data = this.tableData[i]
            if (data.id === this.formEdit.id) {
              this.tableData[i].exp = this.formEdit.exp
              this.tableData[i].addMoney = this.formEdit.addMoney
              this.tableData[i].addVipExp = this.formEdit.addVipExp
              this.tableData[i].activePoint = this.formEdit.activePoint
              this.tableData[i].betLevel = this.formEdit.betLevel
              this.tableData[i].betParam = this.formEdit.betParam
              break
            }
          }
        }
      })
    },
    onImport () {
      // 导入EXCEL
      importLevels(this.results).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          this.$Message.info('导入excel成功!!!')
          this.onSearch()
        }
      })
    },
    onExport () {
      // 导出EXCEL
      getAllLevels().then((data) => {
        if (!data) {
          return
        }

        let allTableData = []
        let LevelVO = getVO('LevelVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = LevelVO.decode(pv.data[i])
          vo.id = vo.id.toNumber()
          vo.exp = vo.exp.toNumber()
          vo.addMoney = vo.addMoney.toNumber()
          vo.addVipExp = vo.addVipExp.toNumber()
          vo.activePoint = vo.activePoint.toNumber()
          allTableData.push(vo)
        }
        console.log(allTableData)
        exportExcel(this.filename, allTableData)
      })
    },
    onFileChange (e) {
      var files = e.target.files || e.dataTransfer.files
      if (!files.length) {
        return
      }
      importExcel(e.target, this.setResults)
    },
    setResults (res) {
      this.results = res
      console.log('this.results:', this.results)
    }
  },
  computed: {
    tableColumns () {
      let cols = [
        {
          title: 'id',
          key: 'id',
          align: 'center',
          fixed: 'left'
        },
        {
          title: 'exp',
          key: 'exp',
          align: 'center',
          fixed: 'left'
        },
        {
          title: 'addMoney',
          key: 'addMoney',
          align: 'center'
        },
        {
          title: 'addVipExp',
          key: 'addVipExp',
          align: 'center'
        },
        {
          title: 'betLevel',
          key: 'betLevel',
          align: 'center'
        },
        {
          title: 'activePoint',
          key: 'activePoint',
          align: 'center'
        },
        {
          title: 'betParam',
          key: 'betParam',
          align: 'center'
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          fixed: 'right',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: () => {
                    let idx = params.index
                    this.showEditLevel(idx)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ]
      return cols
    }
  }
}

</script>

<style scoped>
.log-str{
  font-size: 14px;
  margin-bottom: 2px;
}
.content{
  min-height: 460px;
}
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}

.bottom-table{
  margin-top: 5px;
  margin-bottom: 10px;
}
</style>
